Return to start page

Systems/Character/Struct Spell.j

Code

		
1			/// This library contains the ASpell struct which is used for character spells.
2 /// @author Tamino Dauth
3 library AStructSystemsCharacterSpell requires optional ALibraryCoreDebugMisc, AStructCoreGeneralHashTable, ALibraryCoreEnvironmentUnit, AStructSystemsCharacterAbstractCharacterSystem
4
5 /// @todo vJass bug, should be a part of @struct ASpell.
6 function interface ASpellUpgradeAction takes ASpell spell, integer level returns nothing
7
8 /// @todo vJass bug, should be a part of @struct ASpell.
9 function interface ASpellCastCondition takes ASpell spell returns boolean
10
11 /// @todo vJass bug, should be a part of @struct ASpell.
12 function interface ASpellCastAction takes ASpell spell returns nothing
13
14 /// This struct represents exactly one spell which is owned by a character.
15 /// @author Tamino Dauth
16 struct ASpell extends AAbstractCharacterSystem
17 //start members
18 private integer m_ability
19 private ASpellUpgradeAction m_upgradeAction
20 private ASpellCastCondition m_castCondition
21 private ASpellCastAction m_castAction
22 //members
23 private trigger m_upgradeTrigger
24 private trigger m_castTrigger
25
26 //! runtextmacro optional A_STRUCT_DEBUG("\"ASpell\"")
27
28 //start members
29
30 public method ability takes nothing returns integer
31 return this.m_ability
32 endmethod
33
34 public method upgradeAction takes nothing returns ASpellUpgradeAction
35 return this.m_upgradeAction
36 endmethod
37
38 public method castCondition takes nothing returns ASpellCastCondition
39 return this.m_castCondition
40 endmethod
41
42 public method castAction takes nothing returns ASpellCastAction
43 return this.m_castAction
44 endmethod
45
46 //convenience methods
47
48 public method name takes nothing returns string
49 return GetObjectName(this.m_ability)
50 endmethod
51
52 public method increaseLevel takes nothing returns nothing
53 call IncUnitAbilityLevel(this.unit(), this.m_ability)
54 endmethod
55
56 public method decreaseLevel takes nothing returns nothing
57 call DecUnitAbilityLevel(this.unit(), this.m_ability)
58 endmethod
59
60 public method setLevel takes integer level returns nothing
61 call SetUnitAbilityLevel(this.unit(), this.m_ability, level)
62 endmethod
63
64 public method level takes nothing returns integer
65 return GetUnitAbilityLevel(this.unit(), this.m_ability)
66 endmethod
67
68 public method add takes nothing returns boolean
69 return UnitAddAbility(this.unit(), this.m_ability)
70 endmethod
71
72 public method remove takes nothing returns boolean
73 return UnitRemoveAbility(this.unit(), this.m_ability)
74 endmethod
75
76 //methods
77
78 //Make it available
79 public method enable takes nothing returns nothing
80 call EnableTrigger(this.m_upgradeTrigger)
81 call EnableTrigger(this.m_castTrigger)
82 endmethod
83
84 public method disable takes nothing returns nothing
85 call DisableTrigger(this.m_upgradeTrigger)
86 call DisableTrigger(this.m_castTrigger)
87 endmethod
88
89 private static method triggerConditionRightAbility takes nothing returns boolean
90 local trigger triggeringTrigger = GetTriggeringTrigger()
91 local thistype this = AHashTable.global().handleInteger(triggeringTrigger, "this")
92 local boolean result = (GetLearnedSkill() == this.m_ability)
93 set triggeringTrigger = null
94 return result
95 endmethod
96
97 private static method triggerActionUpgrade takes nothing returns nothing
98 local trigger triggeringTrigger = GetTriggeringTrigger()
99 local thistype this = AHashTable.global().handleInteger(triggeringTrigger, "this")
100 call this.m_upgradeAction.execute(this, GetLearnedSkillLevel())
101 set triggeringTrigger = null
102 endmethod
103
104 /// @todo upgradeAction won't be called correctly
105 private method createUpgradeTrigger takes nothing returns nothing
106 local event triggerEvent
107 local conditionfunc conditionFunction
108 local triggercondition triggerCondition
109 local triggeraction triggerAction
110 set this.m_upgradeTrigger = CreateTrigger()
111 set triggerEvent = TriggerRegisterUnitEvent(this.m_upgradeTrigger, this.unit(), EVENT_UNIT_HERO_SKILL)
112 set conditionFunction = Condition(function thistype.triggerConditionRightAbility)
113 set triggerCondition = TriggerAddCondition(this.m_upgradeTrigger, conditionFunction)
114 set triggerAction = TriggerAddAction(this.m_upgradeTrigger, function thistype.triggerActionUpgrade)
115 call AHashTable.global().setHandleInteger(this.m_upgradeTrigger, "this", this)
116 set triggerEvent = null
117 set conditionFunction = null
118 set triggerCondition = null
119 set triggerAction = null
120 endmethod
121
122 private static method triggerConditionCast takes nothing returns boolean
123 local trigger triggeringTrigger = GetTriggeringTrigger()
124 local thistype this = AHashTable.global().handleInteger(triggeringTrigger, "this")
125 local boolean result = (GetSpellAbilityId() == this.m_ability)
126 if (result) then
127 set result = (this.m_castCondition == 0 or this.m_castCondition.evaluate(this))
128 if (not result) then
129 //taken from wc3jass.com
130 call PauseUnit(this.unit(), true)
131 call IssueImmediateOrder(this.unit(), "stop")
132 call PauseUnit(this.unit(), false)
133 endif
134 endif
135 set triggeringTrigger = null
136 return result
137 endmethod
138
139 private static method triggerActionCast takes nothing returns nothing
140 local trigger triggeringTrigger = GetTriggeringTrigger()
141 local thistype this = AHashTable.global().handleInteger(triggeringTrigger, "this")
142 call this.m_castAction.execute(this)
143 set triggeringTrigger = null
144 endmethod
145
146 private method createCastTrigger takes nothing returns nothing
147 local event triggerEvent
148 local conditionfunc conditionFunction
149 local triggercondition triggerCondition
150 local triggeraction triggerAction
151 set this.m_castTrigger = CreateTrigger()
152 set triggerEvent = TriggerRegisterUnitEvent(this.m_castTrigger, this.unit(), EVENT_UNIT_SPELL_CAST)
153 set conditionFunction = Condition(function thistype.triggerConditionCast)
154 set triggerCondition = TriggerAddCondition(this.m_castTrigger, conditionFunction)
155 set triggerAction = TriggerAddAction(this.m_castTrigger, function thistype.triggerActionCast)
156 call AHashTable.global().setHandleInteger(this.m_castTrigger, "this", this)
157 set triggerEvent = null
158 set conditionFunction = null
159 set triggerCondition = null
160 set triggerAction = null
161 endmethod
162
163 /// @param character Used character.
164 /// @param usedAbility The ability which has to be casted by the unit of the character to run the cast action and which has to be skilled for the unit of the character to run the teach action.
165 public static method create takes ACharacter character, integer usedAbility, ASpellUpgradeAction upgradeAction, ASpellCastCondition castCondition, ASpellCastAction castAction returns thistype
166 local thistype this = thistype.allocate(character)
167 //start members
168 set this.m_ability = usedAbility
169 set this.m_upgradeAction = upgradeAction
170 set this.m_castCondition = castCondition
171 set this.m_castAction = castAction
172
173 call character.addSpell(this)
174
175 if (upgradeAction != 0) then
176 call this.createUpgradeTrigger()
177 endif
178 if (castAction != 0) then
179 call this.createCastTrigger()
180 endif
181 return this
182 endmethod
183
184 private method destroyUpgradeTrigger takes nothing returns nothing
185 call AHashTable.global().destroyTrigger(this.m_upgradeTrigger)
186 set this.m_upgradeTrigger = null
187 endmethod
188
189 private method destroyCastTrigger takes nothing returns nothing
190 call AHashTable.global().destroyTrigger(this.m_castTrigger)
191 set this.m_castTrigger = null
192 endmethod
193
194 public method onDestroy takes nothing returns nothing
195 call this.character().removeSpell(this)
196
197 if (this.m_upgradeAction != 0) then
198 call this.destroyUpgradeTrigger()
199 endif
200 if (this.m_castAction != 0) then
201 call this.destroyCastTrigger()
202 endif
203 endmethod
204
205 public static method enemyTargetLoopCondition takes unit target returns boolean
206 return IsUnitDeadBJ(target) or IsUnitSpellImmune(target)
207 endmethod
208
209 public static method enemyTargetLoopConditionResistant takes unit target returns boolean
210 return thistype.enemyTargetLoopCondition(target) or IsUnitSpellResistant(target)
211 endmethod
212
213 public static method allyTargetLoopCondition takes unit target returns boolean
214 return IsUnitDeadBJ(target)
215 endmethod
216
217 /// @todo Add IsUnitStunned
218 public static method allyChannelLoopCondition takes unit target returns boolean
219 return IsUnitDeadBJ(target)// or
220 endmethod
221 endstruct
222
223 endlibrary